/**
 * Class d'affichage des ArbreAVL.
 */
package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import view.composants.Boutton;
import arbreAbstrait.ArbreAVL;
import arbreAbstrait.ElementAVL;

/**
 * Interface graphique pour tester les Arbre AVL
 * 
 * @author patrick-edouard
 *
 */
public class ArbreFenetre extends JFrame implements ActionListener{
	private JTextArea console;
	private JTextField textvaleurElement;
	private JTextField textIndiceElement;
	private JPanel panPrincipal;
	private JPanel panNouvelElement;
	private JPanel panRotations;
	private JScrollPane panConsole;
	private JLabel labelValeurElement;
	private JLabel labelIndiceElement;
	private Boutton bouttonAjoutElement;
	private Boutton bouttonRotationDroite;
	private Boutton bouttonRotationGauche;
	private Boutton bouttonRotationGaucheDroite;
	private Boutton bouttonRotationDroiteGauche;
	private ArbreAVL arbreAVL;
	private int valeurElement;
	private static final long serialVersionUID = 1L;
	
	public ArbreFenetre()
	{
		super();
		this.initialiseFenetre();
	}
	
	/**
	 * <h1>Methode <b>initialiseFenetre</b></h1>
	 * 
	 * Créer la fenètre de test basique, avec ses bouttons et la console dans un panel.
	 */
	private void initialiseFenetre()
	{
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(500, 800);

		this.bouttonAjoutElement = new Boutton("Ajout Element");
		this.bouttonAjoutElement.addActionListener(this);
		this.bouttonRotationDroite = new Boutton("Rotation Droite");
		this.bouttonRotationDroite.addActionListener(this);
		this.bouttonRotationGauche = new Boutton("Rotation Gauche");
		this.bouttonRotationGauche.addActionListener(this);
		this.bouttonRotationGaucheDroite = new Boutton("Rotation GaucheDroite");
		this.bouttonRotationGaucheDroite.addActionListener(this);
		this.bouttonRotationDroiteGauche = new Boutton("Rotation DroiteGauche");
		this.bouttonRotationDroiteGauche.addActionListener(this);
		
		this.labelValeurElement = new JLabel("Valeur Element :");
		this.labelIndiceElement = new JLabel("Indice rotation :");
		
		this.textIndiceElement = new JTextField(5);
		this.textvaleurElement =  new JTextField(5);
		this.console = new JTextArea();
		this.setConsoleToTextArea();
		this.panRotations = new JPanel();
		this.panConsole = new JScrollPane(this.console);
		this.panConsole.setBounds(25,200,425,400);
		this.panConsole.setBorder(new LineBorder(new Color(255,50,255),2,true));
		this.panRotations.setBounds(250,50,200,125);
		this.panRotations.setBorder(new LineBorder(new Color(255,50,255),1,true));
		
		
		this.panRotations.add(this.labelIndiceElement);
		this.panRotations.add(this.textIndiceElement);
		this.panRotations.add(this.bouttonRotationDroite);
		this.panRotations.add(this.bouttonRotationGauche);
		this.panRotations.add(this.bouttonRotationGaucheDroite);
		this.panRotations.add(this.bouttonRotationDroiteGauche);
		
		this.panPrincipal = new JPanel();
		this.panNouvelElement = new JPanel();
		this.panNouvelElement.add(this.bouttonAjoutElement);
		this.panNouvelElement.add(this.labelValeurElement);
		this.panNouvelElement.add(this.textvaleurElement);
		this.panNouvelElement.setBorder(new LineBorder(new Color(255,50,255),1,true));
		this.panNouvelElement.setBounds(25,50,200,125);
		
		this.panPrincipal.setSize(500, 800);
		this.panPrincipal.setLayout(null);
		this.panPrincipal.add(this.panNouvelElement);
		
		this.panPrincipal.add(this.panRotations);
		
		this.panPrincipal.add(this.panConsole);
		
		this.getContentPane().add(this.panPrincipal);
		this.setVisible(true);
	}
	private void setConsoleToTextArea()
	{
		this.console = new JTextArea();
		//this.console.setBorder(new LineBorder(new Color(255,50,255),3,true));
		this.console.setSize(450, 600);
		PrintStream out = new PrintStream( new ArbreConsole( this.console ) );
		System.setOut( out );
	}
	
	public void initArbreDesequilibre()
	{
		this.arbreAVL.setNoeud(-1, 6);
		this.arbreAVL.setNoeud(-1, 7);
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//int[] tab = {-1,50,17,12,72,23,54,76,9,14,19,67,-1,-1}; 
		int[] tab = {-1,50,17,72,12,23,54,76,9,14,19,-1,-1,67,-1,-1}; 
		ArbreFenetre fa = new ArbreFenetre();
		fa.arbreAVL = new ArbreAVL(64);
		
		for(int i = 0; i<tab.length;++i)
		{
			fa.arbreAVL.setNoeud(tab[i], i);
		}
			
		
		/*for(int i=0; i<12; ++i)
		{
			fa.arbreAVL.setNoeud(i, i);
		}*/
		/*
		fa.arbreAVL.setNoeud(1, 50);
		fa.arbreAVL.setNoeud(2, 17);
		fa.arbreAVL.setNoeud(3, 50);
		*/
		//fa.arbreAVL.afficheArbre(1,0);
		//System.out.println("Rotation !");
		//fa.arbreAVL.rotationDroite(1);
		//fa.arbreAVL.rotationGauche(1);
		//fa.arbreAVL.rotationGaucheDroite(1);
		//fa.initArbreDesequilibre();
		//fa.arbreAVL.ajouter(new ElementAVL(15));
		fa.arbreAVL.reequilibrer(1);
		fa.arbreAVL.afficheArbre(1,0);
		//a.afficheHauteurs();
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.bouttonAjoutElement)
		{
			this.ajouteElement();
		}
		if(e.getSource()==this.bouttonRotationDroite)
		{
			this.RotationDroite();
		}
		if(e.getSource()==this.bouttonRotationGauche)
		{
			this.RotationGauche();
		}
		if(e.getSource()==this.bouttonRotationGaucheDroite)
		{
			this.RotationGaucheDroite();
		}
		if(e.getSource()==this.bouttonRotationDroiteGauche)
		{
			this.RotationDroiteGauche();
		}
	}
	
	private void ajouteElement()
	{
		String tmp = this.textvaleurElement.getText();
		if(tmp!="")
		{
			this.valeurElement=Integer.parseInt(tmp);
			this.arbreAVL.ajouter(new ElementAVL(this.valeurElement));
			this.console.setText("");
			this.arbreAVL.afficheArbre(1, 0);
		}
	}
	private void RotationDroite()
	{
		String tmp = this.textIndiceElement.getText();
		if(tmp!="")
		{
			Integer.parseInt(tmp);
			this.arbreAVL.rotationDroite(Integer.parseInt(tmp));
			this.console.setText("");
			this.arbreAVL.afficheArbre(1, 0);
		}
	}
	private void RotationGauche()
	{
		String tmp = this.textIndiceElement.getText();
		if(tmp!="")
		{
			Integer.parseInt(tmp);
			this.arbreAVL.rotationGauche(Integer.parseInt(tmp));
			this.console.setText("");
			this.arbreAVL.afficheArbre(1, 0);
		}
	}
	private void RotationDroiteGauche()
	{
		String tmp = this.textIndiceElement.getText();
		if(tmp!="")
		{
			Integer.parseInt(tmp);
			this.arbreAVL.rotationDroiteGauche(Integer.parseInt(tmp));
			this.console.setText("");
			this.arbreAVL.afficheArbre(1, 0);
		}
	}
	private void RotationGaucheDroite()
	{
		String tmp = this.textIndiceElement.getText();
		if(tmp!="")
		{
			Integer.parseInt(tmp);
			this.arbreAVL.rotationGaucheDroite(Integer.parseInt(tmp));
			this.console.setText("");
			this.arbreAVL.afficheArbre(1, 0);
		}
	}

}
